﻿using System;
using System.IO;
using System.Text;

namespace CodeRobot.DAL
{
    /// <summary>
    /// 创建常用工具类
    /// </summary>
    public class UtilityHelper
    {
        /// <summary>
        /// 创建日志记录类
        /// </summary>
        /// <param name="strFilePath">保存路径</param>
        /// <param name="strProjectName">项目命名</param>
        public static void CreateLogHelperFile(string strFilePath, string strProjectName)
        {
            Directory.CreateDirectory(strFilePath);
            StreamWriter sw = new StreamWriter(strFilePath + "\\LogHelper.cs", false, Encoding.GetEncoding("utf-8"));
            sw.WriteLine("using System;");
            sw.WriteLine("using log4net;");
            sw.WriteLine("");
            sw.WriteLine("namespace " + CodeRobot.Utility.StringHelper.GetTableNameUpper(strProjectName) + ".Utility");
            sw.WriteLine("{");
            sw.WriteLine("");
            sw.WriteLine("    /// <summary>");
            sw.WriteLine("    /// 版权所有: Copyright © " + DateTime.Now.Year.ToString() + " Cloudin. 保留所有权利。");
            sw.WriteLine("    /// 内容摘要: 日志记录类(注意：如果是Winform程序，必须把log4net.config拷贝到Debug目录下)");
            sw.WriteLine("    /// 完成日期：" + DateTime.Now.ToString("yyyy年M月d日"));
            sw.WriteLine("    /// 版    本：V1.0 ");
            sw.WriteLine("    /// 作    者：Adin.Lee");
            sw.WriteLine("    /// </summary>");
            sw.WriteLine("    public class LogHelper");
            sw.WriteLine("    {");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 记录异常日志");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"t\">通常是类名</param>");
            sw.WriteLine("        /// <param name=\"ex\">异常对象</param>");
            sw.WriteLine("        /// <param name=\"strMessageObject\">通常是方法名或指定记录消息类型</param>");
            sw.WriteLine("        /// <param name=\"strFunctionName\">通常是函数名</param>");
            sw.WriteLine("        /// <param name=\"hasWeb\">是否记录网站日志：true=是，false=否（记录记录应用程序日志）</param>");
            sw.WriteLine("        public static void Error(Type t, Exception ex,string strMessageObject,string strFunctionName,bool hasWeb)");
            sw.WriteLine("        {");
            sw.WriteLine("            string strPath = \"\";");
            sw.WriteLine("            if(hasWeb)");
            sw.WriteLine("            {");
            sw.WriteLine("                strPath = System.Web.HttpContext.Current.Server.MapPath(\"//\") + \"log4net.config\";//WEB");
            sw.WriteLine("            }");
            sw.WriteLine("            else");
            sw.WriteLine("            {");
            sw.WriteLine("                strPath = System.Windows.Forms.Application.StartupPath + \"\\\\log4net.config\";//WinForm");
            sw.WriteLine("            }");
            sw.WriteLine("");
            sw.WriteLine("            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo((strPath)));");
            sw.WriteLine("");
            sw.WriteLine("            ILog ilog = LogManager.GetLogger(t);");
            sw.WriteLine("            if(ilog.IsErrorEnabled)");
            sw.WriteLine("            {");
            sw.WriteLine("                ilog.Error(strMessageObject, ex);");
            sw.WriteLine("            }");
            sw.WriteLine("        }");
            sw.WriteLine("\r\n");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 记录调试日志");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"t\">通常是类名</param>");
            sw.WriteLine("        /// <param name=\"strMessageObject\">通常是方法名或指定记录消息类型</param>");
            sw.WriteLine("        /// <param name=\"strFunctionName\">通常是函数名</param>");
            sw.WriteLine("        /// <param name=\"hasWeb\">是否记录网站日志：true=是，false=否（记录记录应用程序日志）</param>");
            sw.WriteLine("        public static void Debug(Type t, string strMessageObject, string strFunctionName,bool hasWeb)");
            sw.WriteLine("        {");
            sw.WriteLine("            string strPath = \"\";");
            sw.WriteLine("            if(hasWeb)");
            sw.WriteLine("            {");
            sw.WriteLine("                strPath = System.Web.HttpContext.Current.Server.MapPath(\"//\") + \"log4net.config\";//WEB");
            sw.WriteLine("            }");
            sw.WriteLine("            else");
            sw.WriteLine("            {");
            sw.WriteLine("                strPath = System.Windows.Forms.Application.StartupPath + \"\\\\log4net.config\";//WinForm");
            sw.WriteLine("            }");
            sw.WriteLine("");
            sw.WriteLine("            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo((strPath)));");
            sw.WriteLine("");
            sw.WriteLine("            ILog ilog = LogManager.GetLogger(t);");
            sw.WriteLine("            if(ilog.IsDebugEnabled)");
            sw.WriteLine("            {");
            sw.WriteLine("                ilog.Debug(strMessageObject);");
            sw.WriteLine("            }");
            sw.WriteLine("        }");
            sw.WriteLine("\r\n");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 记录信息日志");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"t\">通常是类名</param>");
            sw.WriteLine("        /// <param name=\"strMessageObject\">通常是方法名或指定记录消息类型</param>");
            sw.WriteLine("        /// <param name=\"strFunctionName\">通常是函数名</param>");
            sw.WriteLine("        /// <param name=\"hasWeb\">是否记录网站日志：true=是，false=否（记录记录应用程序日志）</param>");
            sw.WriteLine("        public static void Info(Type t, string strMessageObject, string strFunctionName,bool hasWeb)");
            sw.WriteLine("        {");
            sw.WriteLine("            string strPath = \"\";");
            sw.WriteLine("            if(hasWeb)");
            sw.WriteLine("            {");
            sw.WriteLine("                strPath = System.Web.HttpContext.Current.Server.MapPath(\"//\") + \"log4net.config\";//WEB");
            sw.WriteLine("            }");
            sw.WriteLine("            else");
            sw.WriteLine("            {");
            sw.WriteLine("                strPath = System.Windows.Forms.Application.StartupPath + \"\\\\log4net.config\";//WinForm");
            sw.WriteLine("            }");
            sw.WriteLine("");
            sw.WriteLine("            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo((strPath)));");
            sw.WriteLine("");
            sw.WriteLine("            ILog ilog = LogManager.GetLogger(t);");
            sw.WriteLine("            if(ilog.IsInfoEnabled)");
            sw.WriteLine("            {");
            sw.WriteLine("                ilog.Info(strMessageObject);");
            sw.WriteLine("            }");
            sw.WriteLine("");
            sw.WriteLine("        }");
            sw.WriteLine("\r\n");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 记录警告日志");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"t\">通常是类名</param>");
            sw.WriteLine("        /// <param name=\"strMessageObject\">通常是方法名或指定记录消息类型</param>");
            sw.WriteLine("        /// <param name=\"strFunctionName\">通常是函数名</param>");
            sw.WriteLine("        /// <param name=\"hasWeb\">是否记录网站日志：true=是，false=否（记录记录应用程序日志）</param>");
            sw.WriteLine("        public static void Warn(Type t, string strMessageObject, string strFunctionName,bool hasWeb)");
            sw.WriteLine("        {");
            sw.WriteLine("            string strPath = \"\";");
            sw.WriteLine("            if(hasWeb)");
            sw.WriteLine("            {");
            sw.WriteLine("                strPath = System.Web.HttpContext.Current.Server.MapPath(\"//\") + \"log4net.config\";//WEB");
            sw.WriteLine("            }");
            sw.WriteLine("            else");
            sw.WriteLine("            {");
            sw.WriteLine("                strPath = System.Windows.Forms.Application.StartupPath + \"\\\\log4net.config\";//WinForm");
            sw.WriteLine("            }");
            sw.WriteLine("");
            sw.WriteLine("            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo((strPath)));");
            sw.WriteLine("");
            sw.WriteLine("            ILog ilog = LogManager.GetLogger(t);");
            sw.WriteLine("            if(ilog.IsWarnEnabled)");
            sw.WriteLine("            {");
            sw.WriteLine("                ilog.Warn(strMessageObject);");
            sw.WriteLine("            }");
            sw.WriteLine("        }");
            sw.WriteLine("\r\n");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 记录严重错误日志");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"t\">通常是类名</param>");
            sw.WriteLine("        /// <param name=\"strMessageObject\">通常是方法名或指定记录消息类型</param>");
            sw.WriteLine("        /// <param name=\"strFunctionName\">通常是函数名</param>");
            sw.WriteLine("        /// <param name=\"hasWeb\">是否记录网站日志：true=是，false=否（记录记录应用程序日志）</param>");
            sw.WriteLine("        public static void Fatal(Type t, string strMessageObject, string strFunctionName,bool hasWeb)");
            sw.WriteLine("        {");
            sw.WriteLine("            string strPath = \"\";");
            sw.WriteLine("            if(hasWeb)");
            sw.WriteLine("            {");
            sw.WriteLine("                strPath = System.Web.HttpContext.Current.Server.MapPath(\"//\") + \"log4net.config\";//WEB");
            sw.WriteLine("            }");
            sw.WriteLine("            else");
            sw.WriteLine("            {");
            sw.WriteLine("                strPath = System.Windows.Forms.Application.StartupPath + \"\\\\log4net.config\";//WinForm");
            sw.WriteLine("            }");
            sw.WriteLine("");
            sw.WriteLine("            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo((strPath)));");
            sw.WriteLine("");
            sw.WriteLine("            ILog ilog = LogManager.GetLogger(t);");
            sw.WriteLine("            if(ilog.IsFatalEnabled)");
            sw.WriteLine("            {");
            sw.WriteLine("                ilog.Fatal(strMessageObject);");
            sw.WriteLine("            }");
            sw.WriteLine("        }");
            sw.WriteLine("\r\n");
            sw.WriteLine("    }");
            sw.WriteLine("}");
            sw.Close();

        }


    }
}
